﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Extensions;
using DAL;
using System.Data;

namespace MyDashBoardNS
{
    public class MyDashboard
    {
        public int TaskBoardEntryId { get; set; }
        public DateTime Today { get; set; }
        public string FromTime { get; set; }
        public string ToTime { get; set; }
        public string TaskBoardInfo { get; set; }
        public string Status { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public string SortColumn { get; set; }
        public string SearchText { get; set; }
    }

    public class Settings {
        public string YourName { get; set; }
        public string YourEmail { get; set; }
    }

    public class MyDashboardM
    {
        public string ActionMsg="";
        public DataView dvData;

        public bool SaveTaskBoardDetails(MyDashboard obj)
        {
            bool status = false;
            StringBuilder SQL = new StringBuilder();
            try
            {
                if (obj.TaskBoardEntryId == 0)
                {
                    SQL.Append(" insert into TaskBoard (TaskFromDate,TaskToDate,TaskInfo,Active,Status) ");
                    SQL.Append(" values ('" + obj.Today.ToString("yyyy-M-dd") + " " + obj.FromTime.Replace(".", ":") + ":00'," +  (obj.ToTime==""?"NULL": "'"+obj.Today.ToString("yyyy-M-dd") + " " + obj.ToTime.Replace(" ",":00").Replace(".", ":") + ":00'") + ","+obj.TaskBoardInfo.ToSQLString() + " ,1,'"+obj.Status+"' ) ");
                }
                else
                {
                    SQL.Append(" update TaskBoard SET  TaskFromDate = '" + obj.Today.ToString("yyyy-M-dd") + " " + obj.FromTime.Replace(".", ":") + ":00',TaskToDate = " + (obj.ToTime == "" ? "NULL" : "'" + obj.Today.ToString("yyyy-M-dd") + " " + obj.ToTime.Replace(" ", ":00").Replace(".", ":") + ":00'") + ",TaskInfo= " + obj.TaskBoardInfo.ToSQLString() + ",Status = '" + obj.Status + "'");
                    SQL.Append(" where id = " + obj.TaskBoardEntryId);
                }

                DAC objDAC = new DAC();
                status = objDAC.Save(SQL.ToString());
                ActionMsg = objDAC.ErrorMsg;
            }
            catch (Exception ex)
            {
                ActionMsg = ex.ToString();
                Logger.Log(ActionMsg);
            }

            return status;
        }

        public bool GetTaskBoardEntryList(MyDashboard obj) {
            bool status = false;

            try 
            {
                StringBuilder SQL = new StringBuilder();
                SQL.Append(" select ID,TaskFromDate,TaskToDate,TaskInfo,Status,ROW_NUMBER() OVER (ORDER BY  ID) As SrNo from TaskBoard where 1=1 ");

                if (obj.Today != DateTime.MinValue)
                {
                    SQL.Append(" and datediff(dd,TaskFromDate, '" + obj.Today.Date+"')=0 ");
                }

                if (obj.TaskBoardEntryId > 0) {
                    SQL.Append(" and ID= " + obj.TaskBoardEntryId);
                }

                if (obj.StartTime!=null && obj.StartTime.ToDateTime() != DateTime.MinValue && obj.EndTime!=null && obj.EndTime.ToDateTime() != DateTime.MinValue)
                {
                    SQL.Append(" and TaskFromDate >= '" + obj.StartTime + "' and  TaskToDate <='" + obj.EndTime.ToDateTime().AddDays(1).AddSeconds(-1) + "' ");
                }
                else if (obj.StartTime!=null &&  obj.StartTime.ToDateTime() != DateTime.MinValue)
                {
                    SQL.Append(" and TaskFromDate>= '" + obj.StartTime + "'");
                }
                else if (obj.EndTime!=null && obj.EndTime.ToDateTime() != DateTime.MinValue)
                {
                    SQL.Append(" and TaskToDate <='" + obj.EndTime.ToDateTime().AddDays(1).AddSeconds(-1) + "' ");
                }

                if (obj.SearchText.IsNotNullAndEmpty()) {
                    SQL.Append(" and TaskInfo like '%" + obj.SearchText + "%' ");
                }

                if (obj.SortColumn.IsNotNullAndEmpty()) {
                    //SQL.Append(" order by "+obj.SortColumn+" asc ");
                }

                DAC objDAC = new DAC();
                if (objDAC.Select(SQL.ToString())) {
                    dvData = objDAC.dvData;
                    status = true;
                }

            }catch(Exception ex){
                Logger.Log(ex.ToString());
            }

            return status;
        }

        public bool SaveSettings(Settings obj)
        {
            bool status = false;
            StringBuilder SQL = new StringBuilder();
            try
            {
                SQL.Append(" if exists( select [column] from Settings where [column]='YourName') update Settings set [Value]= " + obj.YourName.ToSQLString() + "  where [Column]='YourName'  else insert into Settings([Column],[Value]) values ('YourName'," + obj.YourName.ToSQLString() + ") ");
                SQL.Append(" if exists( select [column] from Settings where [column]='YourEmail') update Settings set [Value]= " + obj.YourEmail.ToSQLString() + "  where [Column]='YourEmail'  else insert into Settings([Column],[Value]) values ('YourEmail'," + obj.YourEmail.ToSQLString() + ") ");

                DAC objDAC = new DAC();
                status = objDAC.Save(SQL.ToString());
                ActionMsg = objDAC.ErrorMsg;
            }
            catch (Exception ex)
            {
                ActionMsg = ex.ToString();
                Logger.Log(ActionMsg);
            }

            return status;
        }

        public bool GetSettings(Settings obj)
        {
            bool status = false;
            StringBuilder SQL = new StringBuilder();
            try
            {
                SQL.Append(" select [Column],[Value] from settings ");

                DAC objDAC = new DAC();
                if (objDAC.Select(SQL.ToString()))
                {
                    dvData = objDAC.dvData;
                    status = true;
                }
                
                ActionMsg = objDAC.ErrorMsg;
            }
            catch (Exception ex)
            {
                ActionMsg = ex.ToString();
                Logger.Log(ActionMsg);
            }

            return status;
        }

        public bool DBValidation(string constring) {
            bool _ = false;
            try 
            {
                DAC objDAC = null;
                
                if (constring.IsNotNullAndEmpty())
                    objDAC = new DAC(constring);
                else
                    objDAC = new DAC();

                ActionMsg = objDAC.ErrorMsg;
 
                if (objDAC._dbConnection!=null && objDAC._dbConnection.State == ConnectionState.Open)
                {
                    _ = true;
                }

            }catch(Exception ex){
                Logger.Log(ex.ToString());
            }
            return _;
        }

    }
}
